﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.serverpackets;
using MySql.Data.MySqlClient;
using l1cs.server.datatables;

namespace l1cs.server.templates
{
    public class L1BookMark
    {
        private static Logger _log = Logger.getLogger( typeof( L1BookMark ).FullName );

        private int _charId;

        private int _id;

        private String _name;

        private int _locX;

        private int _locY;

        private short _mapId;

        public L1BookMark()
        {
        }

        public static void deleteBookmark( L1PcInstance player, String s )
        {
            L1BookMark book = player.getBookMark( s );
            if ( book != null )
            {
                String sqlstr = "DELETE FROM character_teleport WHERE id=?1";
                using ( MySqlWrapper wrapper = new MySqlWrapper() )
                {
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = book.getId();
                    wrapper.ExecNonQuery( cmd );
                }
            }
        }

        public static void addBookmark( L1PcInstance pc, String s )
        {
            // クライアント側でチェックされるため不要
            //		if (s.length() > 12) {
            //			pc.sendPackets(new S_ServerMessage(204));
            //			return;
            //		}

            if ( !pc.getMap().isMarkable() )
            {
                pc.sendPackets( new S_ServerMessage( 214 ) ); // \f1ここを記憶することができません。
                return;
            }

            int size = pc.getBookMarkSize();
            if ( size > 49 )
            {
                return;
            }

            if ( pc.getBookMark( s ) == null )
            {
                L1BookMark bookmark = new L1BookMark();
                bookmark.setId( IdFactory.getInstance().nextId() );
                bookmark.setCharId( pc.getId() );
                bookmark.setName( s );
                bookmark.setLocX( pc.getX() );
                bookmark.setLocY( pc.getY() );
                bookmark.setMapId( pc.getMapId() );

                String sqlstr = @"INSERT INTO character_teleport SET 
                                    id      = ?1, 
                                    char_id = ?2, 
                                    name    = ?3, 
                                    locx    = ?4, 
                                    locy    = ?5, 
                                    mapid   = ?6";
                using ( MySqlWrapper wrapper = new MySqlWrapper() )
                {
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = bookmark.getId();
                    cmd.Parameters.Add( "?2", MySqlDbType.Int32 ).Value = bookmark.getCharId();
                    cmd.Parameters.Add( "?3", MySqlDbType.String ).Value = bookmark.getName();
                    cmd.Parameters.Add( "?4", MySqlDbType.Int32 ).Value = bookmark.getLocX();
                    cmd.Parameters.Add( "?5", MySqlDbType.Int32 ).Value = bookmark.getLocY();
                    cmd.Parameters.Add( "?6", MySqlDbType.Int32 ).Value = bookmark.getMapId();
                    wrapper.ExecNonQuery( cmd );
                }
                pc.addBookMark( bookmark );
                pc.sendPackets( new S_Bookmarks( s, bookmark.getMapId(), bookmark.getId() ) );
            }
            else
            {
                pc.sendPackets( new S_ServerMessage( 327 ) ); // 同じ名前がすでに存在しています。
            }
        }

        public int getId()
        {
            return _id;
        }

        public void setId( int i )
        {
            _id = i;
        }

        public int getCharId()
        {
            return _charId;
        }

        public void setCharId( int i )
        {
            _charId = i;
        }

        public String getName()
        {
            return _name;
        }

        public void setName( String s )
        {
            _name = s;
        }

        public int getLocX()
        {
            return _locX;
        }

        public void setLocX( int i )
        {
            _locX = i;
        }

        public int getLocY()
        {
            return _locY;
        }

        public void setLocY( int i )
        {
            _locY = i;
        }

        public short getMapId()
        {
            return _mapId;
        }

        public void setMapId( short i )
        {
            _mapId = i;
        }
    }
}
